package com.example.administrator.nl.activity.order;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.nl.R;
import com.example.administrator.nl.base.BaseActivity;
import com.example.administrator.nl.fragment.order.MyOrderFragment;
import com.example.administrator.nl.util.ViewPageFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

// 我的订单
public class MyOrderActivity extends BaseActivity {
    private String TAG = "MyOrderActivity";
    @BindView(R.id.tab_my_order)
    TabLayout tab_my_order;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.iv_finish)
    ImageView iv_finish;

    List<Fragment> mLlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listener();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_my_order;
    }

    @Override
    public void init() {
        tv_title.setText("我的订单");

        tab_my_order.addTab(tab_my_order.newTab().setText("全部"));
        tab_my_order.addTab(tab_my_order.newTab().setText("待付款"));
        tab_my_order.addTab(tab_my_order.newTab().setText("待发货"));
        tab_my_order.addTab(tab_my_order.newTab().setText("待收货"));
        tab_my_order.addTab(tab_my_order.newTab().setText("待评价"));

        MyOrderFragment myOrderFragment1 = new MyOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", "全部");
        myOrderFragment1.setArguments(bundle);

        MyOrderFragment myOrderFragment2 = new MyOrderFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString("type", "待付款");
        myOrderFragment2.setArguments(bundle2);

        MyOrderFragment myOrderFragment3 = new MyOrderFragment();
        Bundle bundle3 = new Bundle();
        bundle3.putString("type", "待发货");
        myOrderFragment3.setArguments(bundle3);

        MyOrderFragment myOrderFragment4 = new MyOrderFragment();
        Bundle bundle4 = new Bundle();
        bundle4.putString("type", "待收货");
        myOrderFragment4.setArguments(bundle4);

        MyOrderFragment myOrderFragment5 = new MyOrderFragment();
        Bundle bundle5 = new Bundle();
        bundle5.putString("type", "待发货");
        myOrderFragment5.setArguments(bundle5);

        mLlist = new ArrayList<>();
        mLlist.add(myOrderFragment1);
        mLlist.add(myOrderFragment2);
        mLlist.add(myOrderFragment3);
        mLlist.add(myOrderFragment4);
        mLlist.add(myOrderFragment5);
        viewPager.setAdapter(new ViewPageFragmentAdapter(getSupportFragmentManager(), mLlist));
    }

    private void listener() {
        tab_my_order.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        break;
                    case 3:
                        viewPager.setCurrentItem(3);
                        break;
                    case 4:
                        viewPager.setCurrentItem(4);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        tab_my_order.getTabAt(0).select();
                        break;

                    case 1:
                        viewPager.setCurrentItem(1);
                        tab_my_order.getTabAt(1).select();
                        break;

                    case 2:
                        viewPager.setCurrentItem(2);
                        tab_my_order.getTabAt(2).select();
                        break;

                    case 3:
                        viewPager.setCurrentItem(3);
                        tab_my_order.getTabAt(3).select();
                        break;

                    case 4:
                        viewPager.setCurrentItem(4);
                        tab_my_order.getTabAt(4).select();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        iv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
